package com.zhejiangzhengyuan.municipal_common.utils.httpUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lin_ju
 * Create on 2019/11/27
 */
public class OriginUtil {
    private final static String[] agent = { "Android", "iPhone", "iPod","iPad", "Windows Phone", "MQQBrowser" }; //定义移动端请求的所有可能类型

    /**
     * 判断User-Agent 是不是来自于手机
     */
    public static boolean checkAgentIsMobile(String ua) {
        boolean flag = false;
        if (!ua.contains("Windows NT") || (ua.contains("Windows NT") && ua.contains("compatible; MSIE 9.0;"))) {
            // 排除 苹果桌面系统
            if (!ua.contains("Windows NT") && !ua.contains("Macintosh")) {
                for (String item : agent) {
                    if (ua.contains(item)) {
                        flag = true;
                        break;
                    }
                }
            }
        }
        return flag;
    }
    public static String getOriginType(String ua) {
        return checkAgentIsMobile(ua)?"mobile":"pc";
    }
    //获取ip地址
    public static String getIP(HttpServletRequest request){
        String ip = request.getRemoteAddr();
        System.out.println("ip:"+ip);
        String headerIP = request.getHeader("x-real-ip");
        if(headerIP == null || "".equals(headerIP) || "null".equals(headerIP)){
            headerIP = request.getHeader("x-forwarded-for");
        }
        System.out.println("headerIP:"+headerIP);
        if(headerIP !=null && !"".equals(headerIP) && !"null".equals(headerIP)){
            ip = headerIP;
        }
        return ip;
    }

}
